
#include "student.h"
#include "middlestudent.h"
#include "Primarystudent.h"

#include<istream>
#include<string>
#include<iostream>

using namespace std;
 
void MiddleStudent::input() {
    PrimaryStudent::input();  // 先调用基类的输入方法
    
    cout << "请输入地理成绩: ";
    cin >> geography;
    cout << "请输入历史成绩: ";
    cin >> history;
    cin.ignore();
}

void MiddleStudent::display() const {
    PrimaryStudent::display();  // 显示基类信息
    cout << "地理成绩: " << geography << endl;
    cout << "历史成绩: " << history << endl;
    cout << "总分: " << calculateTotalScore() << endl;
}

double MiddleStudent::calculateTotalScore() const {
    return PrimaryStudent::calculateTotalScore() + geography + history;
}

void MiddleStudent::save(ofstream& out) const
{
    // 写入学生类型
    string type = "MiddleStudent";
    out.write(type.c_str(), type.size() + 1);

    // 先保存基类（PrimaryStudent）的数据
    PrimaryStudent::save(out);

    // 保存派生类的数据（地理和历史成绩）
    out.write(reinterpret_cast<const char*>(&geography), sizeof(geography));
    out.write(reinterpret_cast<const char*>(&history), sizeof(history));
}

void MiddleStudent::load(ifstream& in)
{ 
    // 先加载基类（PrimaryStudent）的数据
    //PrimaryStudent::load(in);
    char buffer[256];
    in.getline(buffer, 256, '\0');
    in.getline(buffer, 256, '\0');
    studentId = buffer;

    in.getline(buffer, 256, '\0');
    name = buffer;

    in.read(&gender, sizeof(gender));
    in.read(reinterpret_cast<char*>(&age), sizeof(age));

    in.getline(buffer, 256, '\0');
    className = buffer;

    // 读取成绩
    in.read(reinterpret_cast<char*>(&english), sizeof(english));
    in.read(reinterpret_cast<char*>(&math), sizeof(math));
    in.read(reinterpret_cast<char*>(&chinese), sizeof(chinese));



    // 加载派生类的数据（地理和历史成绩）
    in.read(reinterpret_cast<char*>(&geography), sizeof(geography));
    in.read(reinterpret_cast<char*>(&history), sizeof(history));
}

